package com.example.mall.web.controller;

import com.example.mall.domain.Categories;
import com.example.mall.dto.CategoriesDto;
import com.example.mall.exception.ErrorEnumCode;
import com.example.mall.exception.GlobalCustomerException;
import com.example.mall.query.CategoriesQuery;
import com.example.mall.query.PageDTO;
import com.example.mall.result.JSONResult;
import com.example.mall.service.ICategoriesService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/categories")
public class CategoriesController {

    @Autowired
    public ICategoriesService categoriesService;

    /**
     * 分页查询商品分类表信息
     * @param categoriesQuery
     * @return
     */
    @GetMapping("queryPage")
    @ApiOperation(value = "分页查询商品分类表信息",notes = "分页查询商品分类表信息",httpMethod = "GET")
    public JSONResult queryPage(CategoriesQuery categoriesQuery){
        PageDTO<CategoriesDto> categoriesDtoPageDTO = categoriesService.queryPage(categoriesQuery);
        return JSONResult.success("查询成功",categoriesDtoPageDTO);
    }

    /**
     * 根据id查询商品分类表信息
     * @param categoryId
     * @return
     */
    @GetMapping("query/{categoryId}")
    @ApiOperation(value = "根据id查询商品分类表信息",notes = "根据id查询商品分类表信息",httpMethod = "GET")
    public JSONResult query(@PathVariable("categoryId")Long categoryId){
        //1.根据id查询用户信息
        if (categoryId == null){
            throw new GlobalCustomerException(ErrorEnumCode.CATEGORYID_NOT_ID);
        }
        Categories categories = categoriesService.getById(categoryId);
        return JSONResult.success("查询成功",categories);
    }

    /**
     * 根据id查询商品分类表信息
     * @param categoryId
     * @return
     */
    @GetMapping("queryCategoriesById/{categoryId}")
    @ApiOperation(value = "根据id查询商品分类表信息",notes = "根据id查询商品分类表信息",httpMethod = "GET")
    public JSONResult queryCategoriesById(@PathVariable("categoryId")Long categoryId){
        //1.根据id查询用户信息
        if (categoryId == null){
            throw new GlobalCustomerException(ErrorEnumCode.CATEGORYID_NOT_ID);
        }
        Categories categories = categoriesService.queryCategoriesById(categoryId);
        return JSONResult.success("查询成功",categories);
    }

    /**
     * 查询全部商品分类表信息
     * @param
     * @return
     */
    @GetMapping("queryCategoriesList")
    @ApiOperation(value = "查询全部商品分类表信息",notes = "查询全部商品分类表信息",httpMethod = "GET")
    public JSONResult queryCategoriesList(){
        List<Categories> categories = categoriesService.queryCategoriesList();
        return JSONResult.success("查询成功",categories);
    }

    /**
     * 新增商品分类表
     * @param categoriesDto
     * @return
     */
    @PostMapping("insert")
    @ApiOperation(value = "新增商品分类表",notes = "新增商品分类表",httpMethod = "POST")
    public JSONResult insert(@RequestBody CategoriesDto categoriesDto){
        categoriesService.insert(categoriesDto);
        return JSONResult.success("保存成功");
    }

    /**
     * 修改商品分类表信息
     * @param categoriesDto
     * @return
     */
    @PostMapping("update")
    @ApiOperation(value = "修改商品分类表信息",notes = "修改商品分类表信息",httpMethod = "POST")
    public JSONResult update(@RequestBody CategoriesDto categoriesDto){
        categoriesService.update(categoriesDto);
        return JSONResult.success("修改成功");
    }

    /**
     * 删除商品分类表信息
     * @param categoryId
     * @return
     */
    @PostMapping("delete/{categoryId}")
    @ApiOperation(value = "删除商品分类表信息",notes = "删除商品分类表信息",httpMethod = "POST")
    public JSONResult delete(@PathVariable("categoryId") Long categoryId) {
        categoriesService.delete(categoryId);
        return JSONResult.success("删除成功");
    }

    /**
     * 批量删除商品分类表信息
     * @param categoryIds
     * @return
     */
    @PostMapping("deleteBatch")
    @ApiOperation(value = "批量删除商品分类表信息",notes = "批量删除商品分类表信息",httpMethod = "POST")
    public JSONResult deleteAll(@RequestBody List<Long> categoryIds) {
        categoriesService.deleteBatch(categoryIds);
        return JSONResult.success("批量删除成功");
    }
}
